const xlsx = require("xlsx");
const path = require("path");
const utf8 = require('utf8');
const fs  = require("fs");

const keywrodnameconfigpath = path.resolve(__dirname,'config','keywordname.config.json')
const haskeywordnameconfig = fs.existsSync(keywrodnameconfigpath)

let knames = haskeywordnameconfig ? require(keywrodnameconfigpath):[]
knames = knames.length === 0 ? undefined:knames

const keywordnames = knames||[
    "关键字",
    "关键词",
    "keyword",
    "keywords",
    "keyword(s)",
]

const transform = ()=>{

    return new Promise((resolve,reject)=>{
    //set default zipcode
    const zipCode = '90001';
    let p = path.resolve(__dirname,"..","input","query.json")
    let querys = require(p);

    fs.readdir(path.resolve(".","input"),(err,files)=>{
        if(err){
            reject(false);
            return;
        }
        if(files && files.length > 0){
            files.filter(f=>f.endsWith('.xlsx')).forEach((file,idx)=>{
                const filename = path.resolve("input",file);

                const wb = xlsx.readFile(filename,{type:"string"});
                

                wb.SheetNames.forEach((sheetName,idx)=>{
                    
                    var results = []
                    var startIdx = 0;
                    //get sheet
                    const sheet = wb.Sheets[sheetName];
                    //analyze csv
                    const csvs = xlsx.utils.sheet_to_csv(sheet,{FS:",","RS":"\n"});
                    const csv = utf8.decode(utf8.encode(csvs));
                    const rows = csv.split("\n");
                    var findCol = -1;
                    const rs = []
                    rows.forEach((row,idx)=>{
                        const cols = row.split(",");
                        rs.push(cols);
                    })
                    const startfind = (row)=>{
                        if(rs.length > row){
                            /**@type Array */
                            var hs = rs[row]||[];
                            hs.forEach((header,idx)=>{
                                if(keywordnames.includes(header.toLowerCase())){
                                    findCol = idx;
                                    console.log(sheetName+" find keyword column: "+findCol + ' and name: '+ header);
                                    return;
                                }
                            })
                            if(findCol === -1){
                                startIdx ++;
                                startfind(startIdx);
                            }    
                        }                        
                    }

                    startfind(0);

                    if(findCol !== -1){
                        results = rs.map((row,)=>{
                            return row[findCol];
                        }).filter((v)=> v !== '' && !keywordnames.includes((v||"").toLowerCase()))
                    }
                    console.log(`find results: ${JSON.stringify(results)}`.green)
                    // console.clear()
                    /**@type Array */
                    let sheetNames = querys.items.map((item)=>item.sheetName)||[];
                    if(!sheetNames.includes(sheetName) && results.length > 0){
                        querys.items.push({
                            zipcode:zipCode,
                            "sheetName":sheetName,
                            keywords:results
                        })    
                    }
                    })
                })
            fs.writeFileSync(p,JSON.stringify(querys,null,3))
            resolve(true);
        }else{
            reject(false);
        }
    })

    })
}

module.exports = {
    transform
}